package com.lonwin.inspectionmanagement.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lonwin.inspectionmanagement.R;
import com.lonwin.inspectionmanagement.R2;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.di.component.DaggerPointRecordingComponent;
import com.lonwin.inspectionmanagement.di.module.PointRecordingModule;
import com.lonwin.inspectionmanagement.mvp.contract.PatrolContract;
import com.lonwin.inspectionmanagement.mvp.contract.PointRecordingContract;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.CruiseLocusBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.FilterBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.HhkqLhAliasBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.PointHistoryBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.RiverBean;
import com.lonwin.inspectionmanagement.mvp.presenter.PointRecordingPresenter;
import com.lonwin.inspectionmanagement.mvp.ui.adapter.PointRecordingAdapter;
import com.lonwin.inspectionmanagement.mvp.ui.adapter.RiverAdapter;
import com.lonwin.inspectionmanagement.mvp.ui.adapter.TextAdapter;
import com.lonwin.inspectionmanagement.widget.ArrowDownUpView;
import com.lonwin.inspectionmanagement.widget.RecylerPopwindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class PointRecordingActivity extends BaseActivity<PointRecordingPresenter> implements PointRecordingContract.View ,RecylerPopwindow.ShowListener{

    @BindView(R2.id.point_recording_Riverlake)
    ArrowDownUpView pointRecordingRiverlake;
    @BindView(R2.id.year)
    ArrowDownUpView year;
    @BindView(R2.id.point_recording_data_RecyclerView)
    RecyclerView pointRecordingDataRecyclerView;
    private  List<HhkqLhAliasBean.DataBean> mdataBeans;//所有河湖信息
    @BindView(R2.id.vcover)
    View vcover;
    @BindView((R2.id.ll_choice))
    LinearLayout ll_choice;
    private   PointRecordingAdapter adapter;
    private int selectTimeIndex=0;
    private int selectCruiseIndex=0;
    private  TextView tv_title;
    private TextView tv_river;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerPointRecordingComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .pointRecordingModule(new PointRecordingModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_point_recording; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mdataBeans= (List<HhkqLhAliasBean.DataBean>) getIntent().getSerializableExtra(AppConstants.EXTRA_DATA);
        if (mdataBeans!=null){
            intRiver();
        }
        initYear();
        initRvContent();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.firstQuery(getIntent().getStringExtra("xcid"));
    }

    /**
     * 初始化控件
     */
    private void initRvContent() {
        View headerView = LayoutInflater.from(this).inflate(R.layout.river_header,null,false);
         tv_title=(TextView) headerView.findViewById(R.id.tv_title);
        tv_river=(TextView) headerView.findViewById(R.id.tv_river);
         adapter =new PointRecordingAdapter(new ArrayList<>());
        adapter.addHeaderView(headerView);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        ArmsUtils.configRecyclerView(pointRecordingDataRecyclerView,linearLayoutManager);
        pointRecordingDataRecyclerView.setAdapter(adapter);
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter madapter, View view, int position) {
                if (view.getId()==R.id.point_recording_item_point_btn)
                {
                    Intent intent=new Intent(PointRecordingActivity.this,CreatPointRecordActivity.class);
                    intent.putExtra("PointHistoryBeanDataBean",adapter.getItem(position));
                   startActivity(intent);

                }
            }
        });
    }
    private void intRiver() {
        TextAdapter textAdapter=new TextAdapter(mdataBeans);
        RecylerPopwindow<TextAdapter> recylerPopwindow=new RecylerPopwindow<TextAdapter>(textAdapter,this);
        recylerPopwindow.setShowListener(this);
        textAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (FilterBean item:mdataBeans) {
                    item.setSelected(false);
                }
                mdataBeans.get(position).setSelected(true);
                selectCruiseIndex=position;
                textAdapter.notifyDataSetChanged();
                if (recylerPopwindow.isShowing()){
                    recylerPopwindow.dismiss();
                }
                pointRecordingRiverlake.updateTitle(mdataBeans.get(position).getTitle());
                mPresenter.queryPoint(String.valueOf(mdataBeans.get(selectCruiseIndex).getId()),"2018-08");
            }
        });
        recylerPopwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                vcover.setVisibility(View.GONE);
                pointRecordingRiverlake.rotate180Arrow();
            }
        });
        pointRecordingRiverlake.setExpandListener(new ArrowDownUpView.ExpandListener() {
            @Override
            public void expand(View view) {
                recylerPopwindow.showAsDropDown(ll_choice);
            }

            @Override
            public void close(View view) {
                recylerPopwindow.dismiss();
            }
        });
    }

    private void initYear() {
        List<FilterBean> yeardatas = new ArrayList<>();
        yeardatas.add(new FilterBean("2018年1月"));
        yeardatas.add(new FilterBean("2018年2月"));
        yeardatas.add(new FilterBean("2018年3月"));
        yeardatas.add(new FilterBean("2018年4月"));
        yeardatas.add(new FilterBean("2018年5月"));
        yeardatas.add(new FilterBean("2018年6月"));
        yeardatas.add(new FilterBean("2018年7月"));
        yeardatas.add(new FilterBean("2018年8月"));
        TextAdapter yearAdapter=new TextAdapter(yeardatas);
        RecylerPopwindow<TextAdapter> yearPopwindow=new RecylerPopwindow<TextAdapter>(yearAdapter,this);
        yearPopwindow.setShowListener(this);
        yearPopwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                year.rotate180Arrow();
            }
        });
        yearAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (FilterBean item:yeardatas) {
                    item.setSelected(false);
                }
                yeardatas.get(position).setSelected(true);
                selectTimeIndex=position;
                yearAdapter.notifyDataSetChanged();
                if (yearPopwindow.isShowing()){
                    yearPopwindow.dismiss();
                }
                year.updateTitle(yeardatas.get(position).getTitle());
                mPresenter.queryPoint(String.valueOf(mdataBeans.get(selectCruiseIndex).getId()),"2018-11");
            }
        });
        year.setExpandListener(new ArrowDownUpView.ExpandListener() {
            @Override
            public void expand(View view) {
                yearPopwindow.showAsDropDown(ll_choice);
            }

            @Override
            public void close(View view) {
                yearPopwindow.dismiss();
            }
        });
    }



    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this,message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    public void show() {
        vcover.setVisibility(View.VISIBLE);
    }


    @Override
    public void queryPointHistorySuccess(List<PointHistoryBean.DataBean> dataBeans) {
        if (dataBeans==null||dataBeans.isEmpty())
        {
            showMessage("未查询到打点记录");
            return;
        }
        tv_title.setText(dataBeans.get(0).getSystm().substring(0,10)+"打点记录");
        adapter.setNewData(dataBeans);
        tv_river.setText(dataBeans.get(0).getGroupnm());
    }

    @Override
    public Activity getActivity() {
        return this;
    }
}
